import 'package:flutter/material.dart';

class TestMultiChildLayout extends StatefulWidget {
  @override
  _TestMultiChildLayoutState createState() => _TestMultiChildLayoutState();
}

class _TestMultiChildLayoutState extends State<TestMultiChildLayout> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('模板'),
        ),
        body:
            // Container(
            //   child: CustomMultiChildLayout(
            //     delegate: MyMultiLayout(),
            //     children: [
            //       LayoutId(
            //         id: 1,
            //         child: FlutterLogo(
            //           size: 200,
            //         ),
            //       ),
            //       LayoutId(
            //         id: 2,
            //         child: FlutterLogo(),
            //       ),
            //     ],
            //   ),
            // ),
            // 文字下划线demo
            Container(
          color: Colors.orange,
          child: CustomMultiChildLayout(
            delegate: TextUnderlineDelegate(boldSize: 20),
            children: [
              LayoutId(
                  id: "text",
                  child: Text(
                    "在干嘛啊啊啊",
                    style: TextStyle(fontSize: 30),
                  )),
              LayoutId(
                  id: "underline",
                  child: Container(
                    color: Colors.red,
                  )),
            ],
          ),
        ));
  }
}

class TextUnderlineDelegate extends MultiChildLayoutDelegate {
  TextUnderlineDelegate({this.boldSize = 4});

  final double boldSize;

  // 缺点，不能使用textChild的尺寸来绘制尺寸
  @override
  Size getSize(BoxConstraints constraints) {
    // TODO: implement getSize
    return Size(200, 200);
  }

  @override
  void performLayout(Size size) {
    // TODO: implement performLayout
    final Size textChild = layoutChild("text", BoxConstraints.loose(size));
    layoutChild(
        "underline", BoxConstraints.tight(Size(textChild.width, boldSize)));
    double top = (size.height - textChild.height) / 2;
    double left = (size.width - textChild.width) / 2;

    Offset centerOffset = Offset(left, top);
    positionChild("text", centerOffset);
    // 计算居中的距离

    Offset centerOffsetUnderline = Offset(left, top + textChild.height);
    positionChild("underline", centerOffsetUnderline);
  }

  @override
  bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
    // TODO: implement shouldRelayout
    return true;
  }
}

class MyMultiLayout extends MultiChildLayoutDelegate {
  @override
  void performLayout(Size size) {
    late Size size1, size2;
    // TODO: implement performLayout
    if (hasChild(1)) {
      size1 = layoutChild(1, BoxConstraints.loose(size));
      positionChild(1, Offset(0, 0));
    }
    if (hasChild(2)) {
      //紧约束
      size2 = layoutChild(2, BoxConstraints.tight(Size(100, 100)));
      positionChild(2, Offset(size1.width, size1.height));
    }
  }

  @override
  bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
    // TODO: implement shouldRelayout
    return true;
  }
}
